/**
 * creator          Peter
 * creation time    2020/1/8 19:48
 * describe
 * version	        1.0
 * regenerator
 * update time
 * update describe  ${TODO}
 */
package com.example.hhhh;


import android.text.TextUtils;
import android.util.Log;

import com.github.promeg.pinyinhelper.Pinyin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 门禁排序工具类
 */
public class SortUtils {

    private static final String TAG = "SortUtils";

    public static final int TYPE_CHINESES = 0;//汉字
    public static final int TYPE_NUMBER = 1;//数字
    public static final int TYPE_LETTER = 2;//字母
    private static final int TYPE_UNKNOW = -1;//未知类型

    //默认排序
    private static int mChinesesOrder = 0;//汉字排序
    private static int mNumberOrder = 1;//数字排序
    private static int mLetterOrder = 2;//字母排序
    private static int mUnknowOrder = 3;//未知字符排序

    private static int sortTimes = 0;
    //
    public static String[] a = new String[]{"北方大门", "南方大门", "1栋2单元1层门",
            "1栋2单元B1层门", "1栋2单元B2层门", "1栋2单元B3层门", "A栋2单元B3层门", "12栋1单元",
            "B栋2单元B3层门", "test1", "test100", "test9", "test13", "test10", "test3", "test99", "test1%", "test1……",  "test10000%","%adfb", "#sfa "};
//    public static String[] a = new String[]{"test100", "test10000%", "test9", "test13", "test1", "test10", "test3", "test99", "test1%", "test1……"};
//    public static String[] a = new String[]{"test3","test99",  "test1"};


    /**
     * 可自定义排序先后
     *
     * @param ChinesesOrder 中文顺序
     * @param letterOrder   字母顺序
     * @param numberOrder   数字顺序
     * @param unknowOrder   未知符号顺序
     */
    public static void setSortOrder(int ChinesesOrder, int letterOrder, int numberOrder, int unknowOrder) {
        if (ChinesesOrder == letterOrder || letterOrder == numberOrder || ChinesesOrder == numberOrder
                || unknowOrder == ChinesesOrder || unknowOrder == letterOrder || unknowOrder == numberOrder) {
            throw new IllegalStateException("All order can not equal.");
        }
        mChinesesOrder = ChinesesOrder;
        mLetterOrder = letterOrder;
        mNumberOrder = numberOrder;
        mUnknowOrder = unknowOrder;
    }

    /**
     * 排序
     *
     * @param data
     */
//    public static List<String> sortStringArray(List<String> data) {
//        if (data == null || data.size() == 0) {
//            Log.e(TAG, "error, data = null.");
//            return null;
//        }
//        long startTime = System.currentTimeMillis();
//        sortTimes = 0;
//        Collections.sort(data, new Comparator<String>() {
//            @Override
//            public int compare(String str1, String str2) {
//                //主要排序逻辑
//                int shortestLength = (str1.length() < str2.length()) ? str1.length() : str2.length();
//                Log.d(TAG, String.format("s1 = %s,length = %s; s2 = %s,length = %s",
//                        str1, str1.length(), str2, str2.length()));
//                for (int i = 0; i < shortestLength; i++) {
//                    Log.d(TAG, "sort times:" + sortTimes++);
//                    char c1 = str1.charAt(i);
//                    char c2 = str2.charAt(i);
//                    int type1 = recognizeType(c1);
//                    int type2 = recognizeType(c2);
//                    int typeOrder1 = getTypeOrder(type1);
//                    int typeOrder2 = getTypeOrder(type2);
//                    Log.d(TAG, String.format("c1 = %s, c2 = %s; type1 = %s, type2 = %s; order1 = %s, order2 = %s",
//                            c1, c2, type1, type2, typeOrder1, typeOrder2));
//                    if (typeOrder1 > typeOrder2) {
//                        Log.d(TAG, "return 1");
//                        return 1;
//                    } else if (typeOrder1 < typeOrder2) {
//                        Log.d(TAG, "return -1");
//                        return -1;
//                    } else {
//                        //同类型
//                        if (type1 == TYPE_CHINESES) {
//                            //中文排序
//                            String py1 = getChinesesFirstPingYin(c1);
//                            String py2 = getChinesesFirstPingYin(c2);
//                            if (py1.charAt(0) == py2.charAt(0)) {
//                                continue;
//                            }
//                            return py1.charAt(0) - py2.charAt(0);
//                        } else if (type1 == TYPE_NUMBER) {
//                            //数字排序
//                            String substr1 = str1.substring(i);
//                            String substr2 = str2.substring(i);
//                            boolean isNumber1 = isNumber(substr1);
//                            boolean isNumber2 = isNumber(substr2);
//                            Log.e(TAG, String.format("substr1.name = %s, substr2.name = %s; isNumber1 = %s, isNumber2 = %s",
//                                    substr1, substr2, isNumber1, isNumber2));
//                            if (isNumber1 && isNumber2) {
//                                //后缀全是纯数字的处理方法
//                                return Integer.valueOf(substr1) - Integer.valueOf(substr2);
//                            } else if (isNumber1 || isNumber2) {
//                                //后缀其中一个是纯数字的时候，直接跳到后面类型对比
//                                if ((i == shortestLength - 1) && (str1.length() != str2.length())) {
//                                    //判断到短数据最后一个都无结果时，直接排序长数据在后(解决类似test1, test10000%的排序问题)
//                                    return str1.length() - str2.length();
//                                } else {
//                                    continue;
//                                }
//                            } else {
//                                //后缀两个都不是纯数字的处理
//                            }
//                            if (Integer.valueOf(c1) == Integer.valueOf(c2)) {
//                                //继续往后对比
//                                continue;
//                            }
//                            if (str1.length() > str2.length()) {
//                                return 1;
//                            } else if (str1.length() < str2.length()) {
//                                return -1;
//                            }
//                            return Integer.valueOf(c1) - Integer.valueOf(c2);
//                        } else if (type1 == TYPE_LETTER) {
//                            //字母排序
//                            if (c1 == c2) {
//                                continue;
//                            }
//                            return c1 - c2;
//                        } else {
//                            //未知字符同级
//                            return 0;
//                        }
//                    }
//                }
//                return 0;
//            }
//        });
//        Log.e(TAG, String.format("Sort cost time: " + (System.currentTimeMillis() - startTime)));
//        return data;
//    }

    /**
     * 排序
     *
     * @param data
     */
    public static ArrayList<AccessInfo> sortStringArray(ArrayList<AccessInfo> data) {
        if (data == null || data.size() == 0) {
//            Log.e(TAG, "error, data = null.");
            return null;
        }
        long startTime = System.currentTimeMillis();
        sortTimes = 0;
        Collections.sort(data, new Comparator<AccessInfo>() {
            @Override
            public int compare(AccessInfo str1, AccessInfo str2) {

                String s1 = !TextUtils.isEmpty(str1.customizeName) ? str1.customizeName:str1.name;
                String s2 = !TextUtils.isEmpty(str2.customizeName) ? str2.customizeName:str2.name;

                //主要排序逻辑
                int shortestLength = (s1.length() < s2.length()) ? s1.length() : s2.length();
                Log.d(TAG, String.format("s1 = %s,length = %s; s2 = %s,length = %s",
                        s1, s1.length(), s2, s2.length()));
                for (int i = 0; i < shortestLength; i++) {
                    Log.d(TAG, "sort times:" + sortTimes++);
                    char c1 = s1.charAt(i);
                    char c2 = s2.charAt(i);
                    int type1 = recognizeType(c1);
                    int type2 = recognizeType(c2);
                    int typeOrder1 = getTypeOrder(type1);
                    int typeOrder2 = getTypeOrder(type2);
                    Log.d(TAG, String.format("c1 = %s, c2 = %s; type1 = %s, type2 = %s; order1 = %s, order2 = %s",
                            c1, c2, type1, type2, typeOrder1, typeOrder2));
                    if (typeOrder1 > typeOrder2) {
                        Log.d(TAG, "return 1");
                        return 1;
                    } else if (typeOrder1 < typeOrder2) {
//                        Log.d(TAG, "return -1");
                        return -1;
                    } else {
                        //同类型
                        if (type1 == TYPE_CHINESES) {
                            //中文排序
                            String py1 = getChinesesFirstPingYin(c1);
                            String py2 = getChinesesFirstPingYin(c2);
                            if (py1.charAt(0) == py2.charAt(0)) {
                                continue;
                            }
                            return py1.charAt(0) - py2.charAt(0);
                        } else if (type1 == TYPE_NUMBER) {
                            //数字排序
                            String substr1 = s1.substring(i);
                            String substr2 = s2.substring(i);
                            boolean isNumber1 = isNumber(substr1);
                            boolean isNumber2 = isNumber(substr2);
                            Log.e(TAG, String.format("substr1.name = %s, substr2.name = %s; isNumber1 = %s, isNumber2 = %s",
                                    substr1, substr2, isNumber1, isNumber2));
                            if (isNumber1 && isNumber2) {
                                //后缀全是纯数字的处理方法
                                return Integer.valueOf(substr1) - Integer.valueOf(substr2);
                            } else if (isNumber1 || isNumber2) {
                                //后缀其中一个是纯数字的时候，直接跳到后面类型对比
                                if ((i == shortestLength - 1) && (s1.length() != s2.length())) {
                                    //判断到短数据最后一个都无结果时，直接排序长数据在后(解决类似test1, test10000%的排序问题)
                                    return s1.length() - s2.length();
                                } else {
                                    continue;
                                }
                            } else {
                                //后缀两个都不是纯数字的处理
                            }
                            if (Integer.valueOf(c1) == Integer.valueOf(c2)) {
                                //继续往后对比
                                continue;
                            }
                            if (s1.length() > s2.length()) {
                                return 1;
                            } else if (s1.length() < s2.length()) {
                                return -1;
                            }
                            return Integer.valueOf(c1) - Integer.valueOf(c2);
                        } else if (type1 == TYPE_LETTER) {
                            //字母排序
                            if (c1 == c2) {
                                continue;
                            }
                            return c1 - c2;
                        } else {
                            //未知字符同级
                            return 0;
                        }
                    }
                }
                return 0;
            }
        });
//        Log.e(TAG, String.format("Sort cost time: " + (System.currentTimeMillis() - startTime)));

        return data;
    }

    //获取字符从第一个开始除了数字以外的其他类目类型值
    private static int getOtherTypeWithoutNumber(String content) {
        if (content.isEmpty()) {
            return TYPE_UNKNOW;
        }
        for (char c : content.toCharArray()) {
            int type = recognizeType(c);
            if (TYPE_NUMBER != type) {
                return type;
            }
        }
        return TYPE_NUMBER;
    }

    //检查字符串是否是数字
    private static boolean isNumber(String str) {
        if (str.isEmpty()) {
            return false;
        }
        for (char c : str.toCharArray()) {
            int type = recognizeType(c);
            if (TYPE_NUMBER != type) {
                return false;
            }
        }
        return true;
    }

    /**
     * 识别字符的类型
     *
     * @param c
     * @return
     */
    public static int recognizeType(char c) {
        String txt = String.valueOf(c);
        if (txt.isEmpty()) {
//            Log.d(TAG, "error, unknow text type.");
            return TYPE_UNKNOW;
        }
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(txt);
        if (m.matches()) {
            return TYPE_NUMBER;
        }
        p = Pattern.compile("[a-zA-Z]");
        m = p.matcher(txt);
        if (m.matches()) {
            return TYPE_LETTER;
        }
        p = Pattern.compile("[\\u4E00-\\u9FA5]+");
        m = p.matcher(txt);
        if (m.matches()) {
            return TYPE_CHINESES;
        }
//        Log.d(TAG, "error2, unknow text type.");
        return TYPE_UNKNOW;
    }

    /**
     * 获取汉字首拼音
     */
    public static String getChinesesFirstPingYin(char ChineseChar) {

        return Pinyin.toPinyin(ChineseChar);

//        return "";

    }

    private static int getTypeOrder(int type) {
        switch (type) {
            case TYPE_UNKNOW:
                return mUnknowOrder;
            case TYPE_CHINESES:
                return mChinesesOrder;
            case TYPE_NUMBER:
                return mNumberOrder;
            case TYPE_LETTER:
                return mLetterOrder;
        }
        return mUnknowOrder;
    }

}
